package com.shortlink.controller;

import com.shortlink.pojos.dto.dataobject.ResponseResult;
import com.shortlink.pojos.dto.req.UserLoginReqDTO;
import com.shortlink.pojos.dto.req.UserReqDTO;
import com.shortlink.pojos.dto.resp.UserLoginRespDTO;
import com.shortlink.pojos.dto.resp.UserRespDTO;
import com.shortlink.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@Api(tags = "用户模块")
public class UserController {
    @Autowired
    IUserService iUserService;

    /**
     * 用户登录
     * @param userLoginReqDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("用户登录")
    public ResponseResult<UserLoginRespDTO> login(@RequestBody @Validated UserLoginReqDTO userLoginReqDTO){
        UserLoginRespDTO userLoginRespDTO=iUserService.login(userLoginReqDTO);
        return ResponseResult.okResult(userLoginRespDTO);
    }

    @GetMapping("/logout")
    @ApiOperation("用户登出")
    public ResponseResult logout(){
        iUserService.logout();
        return ResponseResult.okResult();
    }

    @PostMapping("/register")
    @ApiOperation("用户注册")
    public ResponseResult register(@RequestBody @Validated UserLoginReqDTO userLoginReqDTO){
        iUserService.register(userLoginReqDTO);
        return ResponseResult.okResult();
    }

    /**
     * 逻辑删除
     * @return
     */
    @PutMapping("/cancel")
    @ApiOperation("用户销户")
    public ResponseResult cancel(){
        iUserService.cancelUser();
        return ResponseResult.okResult();
    }

    @PutMapping("/update")
    @ApiOperation("用户修改个人信息")
    public ResponseResult update(@RequestBody UserReqDTO userReqDTO){
        iUserService.updateUser(userReqDTO);
        return ResponseResult.okResult();
    }

    @GetMapping("/mine")
    @ApiOperation("查看个人信息")
    public ResponseResult<UserRespDTO> mine(){
        UserRespDTO userRespDTO=iUserService.mine();
        return ResponseResult.okResult(userRespDTO);
    }

    /**
     * 注册/更改用户名时使用
     * @param username
     * @return
     */
    @GetMapping("/username/{username}")
    @ApiOperation("用户名是否可用")
    public ResponseResult<Boolean> hasUsername(@PathVariable String username){
        Boolean flag = iUserService.hasUsername(username);
        return ResponseResult.okResult(flag);
    }

}
